var that,
  api = new(require('../../utils/api.js')),
  headimg = api.getimgurl(),
  staticimg = api.getstaticimgurl(),
  QQMapWX = require('../../utils/qqmap-wx-jssdk.js'),
qqmapsdk,
  util = new(require('../../utils/util.js'));
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pagesize:15,
    start:0,
    switchindex:1,//state:'ALL',
    weightarr:['10-25公斤','25-50公斤','50-250公斤','250公斤以上'],
    cancelreason:[
      {
        value:'其他',
        checked:!1,
        id:1,
      },
      {
        value:'不小心点错',
        checked:!1,
        id:2,
      },
      {
        value:'价格太低',
        checked:!1,
        id:3,
      },
      {
        value:'态度太差',
        checked:!1,
        id:4,
      },
      {
        value:'太久没上门',
        checked:!1,
        id:5,
      },
    ],
    cid:0,
    iscancel:!1,
    iscleck:!1,
    list:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    
    that = this;
    let usertype = wx.getStorageSync('user').type,{switchindex}=that.data;
    that.setData({usertype})
    // that.getlist()
    qqmapsdk = new QQMapWX({
      key: getApp().globalData.mapkey // 必填
    });
    if (app.logininfo) {
      that.getlist(switchindex)
    } else {
      util.setwatcher(app, this.afterlogin)
    }
  },
  afterlogin: {
    logininfo: res => {
      let {switchindex}=that.data;
      that.getlist(switchindex)
    } 
  },
  call(e){
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone,
    })
  },
  tonav(e){
    let plugin = requirePlugin('routePlan');
    let key = 'CB5BZ-6NP6W-KQGRJ-OENGJ-AD74O-4FFMC';  //使用在腾讯位置服务申请的key
    let referer = '回收小程序';   //调用插件的app的名称
    let endPoint = JSON.stringify({  //终点
      
      'name': e.currentTarget.dataset.address,
      'latitude': parseFloat(e.currentTarget.dataset.lat) ,
      'longitude': parseFloat(e.currentTarget.dataset.lng)
    });
    wx.navigateTo({
      url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint=' + endPoint+'&navigation=1'
    });
  },
  switch(e){
    let {code}=e.currentTarget.dataset
    that.setData({switchindex:code,start:0,list:[]},()=>{
      that.getlist(code);
    })
  },
  getlist(type){
    wx.showLoading({
      title: '加载中',
    })
    let{pagesize,start,list} = this.data,data = {type,pagesize,start};
    api.getorderlistbystate(data,res=>{
      wx.hideLoading({
        complete: (res) => {},
      })
      if(res.data.issuccess==1){
        let getlist = res.data.list;
        if(getlist.length>0){
          getlist.forEach((x,i)=>{
            console.log(x,'listlist');
            getlist[i]['weight'] = that.data.weightarr[x.apply.weightsection-1]
            //ORDERING未接单 ONMYWAY在路上 CANCELED已取消  CONFIRM已确认  ASSESSED已评价 FINISHED已完成 
            getlist[i]['apply']['statestr'] = 
            x.apply.state=='ORDERING'?'未接单':
            x.apply.state=='ONMYWAY'?'订单已被接，请耐心等待回收员上门':
            x.apply.state=='CANCELED'?'已取消':
            x.apply.state=='ASSESSED'?'已评价':
            x.apply.state=='CONFIRM'?'已确认，待评价':
            "已完成"
            getlist[i]['apply']['date'] =util.toDate1(x.orderdate)//util.formatTimenosecend(new Date(x.orderdate)) 
          })
        }
        list=list.concat(getlist)
        that.setData({list,reachbtm:list.length==Number(res.data.total)})
      }
    })
  },
  tocheckcode(e){
    that.setData({selectitem:e.currentTarget.dataset.item},()=>{
      wx.navigateTo({
        url: '../checkcode/checkcode',
      })
    })
  },
  toassess(e){
      wx.navigateTo({
        url: '../assess/assess?num='+e.currentTarget.dataset.ordernum,
      })
  },
  toaddress(e){
    let{lat,lng}= e.currentTarget.dataset;
    wx.openLocation({
      latitude:Number(lat) ,
      longitude: Number(lng),
    })
  },
  toorderdetail(e){
    let {ordernum} = e.currentTarget.dataset
    wx.navigateTo({
      url: '../finishorderdetail/finishorderdetail?ordernum='+ordernum,
    })
  },
  copy(e){
    wx.setClipboardData({
      data: e.currentTarget.dataset.code,
    })
  },
  // 申请调单
  apply(e){
    let {ordernum}=e.currentTarget.dataset, {switchindex}=that.data;
    wx.showModal({
      title: '申请调单',
      content: '确认申请调单吗？',
      success:function(res){
        if(res.confirm){
          api.setturning(ordernum,res=>{
            if(res.data.issuccess==1){
              wx.showToast({
                title: '申请调单成功',
                icon: 'none',
                duration: 2000
              })
              that.getlist(switchindex)
            }
          })
         
        }
      },
    })
  },
  // 取消订单
  cancel(e){
    let {ordernum}=e.currentTarget.dataset, {switchindex}=that.data;
    that.setData({ordernum,iscancel:!0})
    // wx.showModal({
    //   title: '取消订单',
    //   content: '确认取消订单吗？',
    //   success:function(res){
    //     if(res.confirm){
    //       api.cancelorder(ordernum,res=>{
    //         if(res.data.issuccess==1){
    //           wx.showToast({
    //             title: '取消订单成功',
    //             icon: 'none',
    //             duration: 2000
    //           })
    //           that.getlist(switchindex)
    //         }
    //       })
         
    //     }
    //   },
    // })
   
  },
  close(e){
    let {ordernum}=e.currentTarget.dataset, {switchindex}=that.data;
    wx.showModal({
      title: '删除订单',
      content: '确认删除订单吗？',
      success:function(res){
        if(res.confirm){
          api.delorder(ordernum,res=>{
            if(res.data.issuccess==1){
              wx.showToast({
                title: '取消删除成功',
                icon: 'none',
                duration: 2000
              })
              that.getlist(switchindex)
            }else{
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
                duration: 2000
              })
            }
          })
         
        }
      },
    })
  },
  radioChange(e){
    let {cancelreason,cid}=that.data,{value}=e.detail
    if(cid==value)return;
    cancelreason.forEach(x=>{
      if(x.id==value){
        x.checked=!0
      }else{
        x.checked=!1
      }
    })
    that.setData({cid:value,cancelreason})
    
  },
  determine(){
    let {cancelreason,cid,ordernum,switchindex,iscleck}=that.data,reason=''
    if(iscleck)return;
    that.setData({iscleck:!0})
    
    if(cid==0){
      wx.showModal({
        content:'请选择取消原因',
        showCancel:!1,
      })
      that.setData({iscleck:!1})
      return;
    }
    cancelreason.forEach(x=>{
      if(x.id==cid){
        reason=x.value
      }
    })
    
    console.log(1111111);
    api.cancelorder({number:ordernum,reason},res=>{
        if(res.data.issuccess==1){
          wx.showToast({
            title: '取消订单成功',
            icon: 'none',
            duration: 2000
          })
          that.setData({iscleck:!1},()=>{
            that.getlist(switchindex)
          })
        }else{
          that.setData({iscleck:!1})
        }
        that.setData({iscancel:!1,})
    })
  },
  hidemodal(){
    that.setData({iscancel:!1})
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(that.data.needfresh){
      let {switchindex}=that.data;
      that.getlist(switchindex)
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let{switchindex,reachbtm,start,pagesize} = this.data;
        if(reachbtm){
          wx.showToast({
            title: '已经到底了',
            icon:'none'
          })
          return;
        }else{
          start=pagesize+start
          that.setData({start},()=>{
            that.getlist(switchindex)
          })

        }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})